# -*-coding:Utf-8 -*
""" Childs systeme
"""

class Twin(object):

	def __init__(self, twin, settings, stats):
		if twin == None:
			self.settings = settings
			self.stats = stats
			return
		if not hasattr(twin,'__refresh_link__') or not callable(twin.__refresh_link__):
			raise TypeError("twin object must have __refresh_link__ methode")
		if hasattr(twin,'settings') and type(twin.settings) == dict:
			settings.update(twin.settings)
		if hasattr(twin,'stats') and type(twin.stats) == dict:
			stats.update(twin.stats)
		self._last_twin_update_ = twin.__update_twin__
		self.__update_twin__(settings,stats)
		linfunc = self.__refresh_link__
		self.__refresh_link__ = twin.__refresh_link__
		twin.__refresh_link__ = linfunc
		
	def _last_twin_update_(self,settings,stats):
		return
		
	def __update_twin__(self,newSettings,newStats):
		self.settings = newSettings
		self.stats = newStats
		self._last_twin_update_(newSettings,newStats)
		
	def __link_stable__(self):
		return False
		
	def __refresh_link__(self, launchOb = None):
		if self == launchOb:
			return
		if launchOb == None:
			launchOb = self
		if not self.__link_stable__():
			self.__refresh_link__(launchOb)
